﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace MPNotificationBar
{
    public class Notification : INotification
    {
        /// <summary>
        /// Progress Changed event
        /// </summary>
        public event EventHandler ProgressChanged;
        public event EventHandler ContentChanged;
        string _Text = "";
        string _SubText = "";
        short _Progress = 0;
        Boolean _RemoveNotification = false;
        int _PluginID = 0;
        Boolean _NeedsProgressBar = false;
        NotificationBarManager.Types _NotificationType = NotificationBarManager.Types.Information;
        int _NotificationID = 0;
        Timer _SecondsToLiveTimer;
        Boolean _ShowInFullScreen = false;
        String _ThumbnailPath = "";

        public Notification(int PuginID)
        {
            _PluginID = PluginID;
        }

        public Notification(int PuginID, string Text, NotificationBarManager.Types NotificationType)
            : this(PuginID)
        {
            _Text = Text;
            _NotificationType = NotificationType;
        }

        public Notification(int PuginID, string Text, NotificationBarManager.Types NotificationType, Boolean NeedsProgressBar)
            : this(PuginID, Text, NotificationType)
        {
            _NeedsProgressBar = NeedsProgressBar;
        }

        public Notification(int PuginID, string Text, NotificationBarManager.Types NotificationType, Boolean NeedsProgressBar, Boolean ShowInFullScreen)
            : this(PuginID, Text, NotificationType, NeedsProgressBar)
        {
            _ShowInFullScreen = ShowInFullScreen;
        }

        public Notification(int PuginID, string Text, string SubText, string ThumbnailPath, int SecondsToLive, NotificationBarManager.Types NotificationType, Boolean NeedsProgressBar, Boolean ShowInFullScreen)
            : this(PuginID, Text, NotificationType, NeedsProgressBar, ShowInFullScreen)
        {
            this._SubText = SubText;
            this._ThumbnailPath = ThumbnailPath;
            this.SecondsToLive = SecondsToLive;
        }

        /// <summary>
        /// Gets/sets notification id
        /// </summary>
        /// <returns>string</returns>
        public int NotificationID
        {
            get { return _NotificationID; }
            set
            {
                _NotificationID = value;
            }
        }
        /// <summary>
        /// Gets/sets text to display
        /// </summary>
        /// <returns>string</returns>
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }
        /// <summary>
        /// Gets/sets sub text to display
        /// </summary>
        /// <returns>string</returns>
        public string SubText
        {
            get { return _SubText; }
            set { _SubText = value; }
        }
        /// <summary>
        /// Gets/sets progress (0-100)
        /// </summary>
        /// <returns>Int16</returns>
        public short Progress
        {
            get { return _Progress; }
            set
            {
                _Progress = value;
                try
                {
                    ProgressChanged(this, EventArgs.Empty);
                }
                catch { }
            }
        }
        /// <summary>
        /// Gets/sets plugin id
        /// </summary>
        /// <returns>Int32</returns>
        public int PluginID
        {
            get { return _PluginID; }
            set { _PluginID = value; }
        }
        /// <summary>
        /// Gets/sets value indicating if notification uses progressbar 
        /// </summary>
        /// <returns>Boolean</returns>
        public bool IsUsingProgressBar
        {
            get { return _NeedsProgressBar; }
            set { _NeedsProgressBar = value; }
        }
        /// <summary>
        /// Gets/sets value indicating if notification shall be removed
        /// </summary>
        /// <returns>Boolean</returns>
        public bool Remove
        {
            get { return _RemoveNotification; }
            set { _RemoveNotification = value; }
        }
        /// <summary>
        /// Gets/sets notification type
        /// </summary>
        /// <returns>MPNotificationBar.NotificationBarManager.Types</returns>
        public MPNotificationBar.NotificationBarManager.Types NotificationType
        {
            get { return _NotificationType; }
            set { _NotificationType = value; }
        }
        /// <summary>
        /// Seconds to live
        /// </summary>
        public int SecondsToLive
        {
            get
            {
                if (_SecondsToLiveTimer != null)
                    return (int)(_SecondsToLiveTimer.Interval / 1000);
                else
                    return 0;
            }
            set
            {
                if (_SecondsToLiveTimer == null)
                {
                    //Set new timer
                    _SecondsToLiveTimer = new Timer(value * 1000);
                    _SecondsToLiveTimer.Elapsed += new ElapsedEventHandler(_SecondsToLiveTimer_Elapsed);
                    _SecondsToLiveTimer.Enabled = true;
                    _SecondsToLiveTimer.Start();
                }
                else
                {
                    //Reset timer
                    _SecondsToLiveTimer.Stop();
                    _SecondsToLiveTimer.Interval = value * 1000;
                    _SecondsToLiveTimer.Start();
                }
            }
        }
        /// <summary>
        /// Gets/sets value indicating if notification shall show in fullscreen mode
        /// </summary>
        /// <returns>Boolean</returns>
        public bool ShowInFullScreen
        {
            get { return _ShowInFullScreen; }
            set { _ShowInFullScreen = value; }
        }
        /// <summary>
        /// Gets/sets thumbnail path
        /// </summary>
        /// <returns>string</returns>
        public string ThumbnailPath
        {
            get { return _ThumbnailPath; }
            set { _ThumbnailPath = value; }
        }

        void _SecondsToLiveTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _SecondsToLiveTimer.Stop();
            this.Remove = true;
        }
    }
}
